package com.chebaohui.iqs.service.impl.deal.libao.util;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.config.CompanyValue;
import com.chebaohui.iqs.config.InsuranceCode;
import com.chebaohui.iqs.entity.base.Insurance;
import com.chebaohui.iqs.entity.req.CarPrice;
import com.chebaohui.iqs.service.impl.deal.util.TimeBase;

/**
 * 利宝车险保费计算-参数
 * 
 * @author xielonghai
 *
 */
public class ParamtenGet {
	// 商业险选择情况
	private boolean bus = false;
	// 交强险选择情况
	private boolean tra = false;

	/** 商业险投保单号 */
	private String busNo = "";
	/** 商业险开始日期yyyy-MM-dd */
	private String busTimeStartDate = "";
	/** 商业险开始日期小时 */
	private int busTimeStartHour = 0;

	/** 交强险投保单号 */
	private String traNo = "";
	/** 交强险开始日期yyyy-MM-dd */
	private String traTimeStartDate = "";
	private int traTimeStartHour = 0;

	// 利宝带出--车辆信息
	private JSONObject car;

	// 投保单信息
	private JSONObject tprpTmainDto = new JSONObject();
	// 投保标的信息
	private JSONObject tprpTitemCarDto = new JSONObject();
	// 车主信息
	private JSONObject tprptCarOwnerDto = new JSONObject();
	// 投保人信息
	private JSONObject tprptApplicantDto = new JSONObject();
	// 被保险人信息
	private JSONObject tprptInsuredDto = new JSONObject();
	// 险别选择信息List
	private JSONArray tprpTitemKindListDto = new JSONArray();

	/** 保险选择信息信息 */
	private CarPrice priceRequest;

	/**
	 * 构造器(车主信息、车险选择信息、利宝车辆信息)
	 * 
	 * @param ower
	 * @param insurance
	 * @param car
	 */
	public ParamtenGet(CarPrice priceRequest, JSONObject car) throws Exception {
		this.car = car;
		this.priceRequest = priceRequest;
		this.tra = (null != getIns(InsuranceCode.TP.getCode()));
		this.bus = (null != getIns(InsuranceCode.BTR.getCode()));
		if (bus) {//对应的商业险
			this.busTimeStartDate = TimeBase.getDay("yyyy-MM-dd", CompanyValue.nextDay());
			this.busTimeStartHour = 0;
		}

		if (tra) {//对应的交强险
			this.traTimeStartDate = TimeBase.getDay("yyyy-MM-dd", CompanyValue.nextDay());
			this.traTimeStartHour = 0;
		}

	}

	/**
	 * 车险选择信息获取
	 * 
	 * @param code
	 * @return
	 */
	private Insurance getIns(String code) {
		List<Insurance> list = priceRequest.getInsuranceList();
		if (null == list || list.isEmpty())
			return null;
		for (Insurance ins : list) {
			if (ins.getCode().equals(code))
				return ins;
		}
		return null;
	}

	/**
	 * 商业险选择情况
	 * 
	 * @return
	 */
	public boolean isBus() {
		return bus;
	}

	/**
	 * 交强险选择情况
	 * 
	 * @return
	 */
	public boolean isTra() {
		return tra;
	}

	/**
	 * 商业险开始日期yyyy-MM-dd
	 * 
	 * @return
	 */
	public String getBusTimeStartDate() {
		return busTimeStartDate;
	}

	/**
	 * 商业险投保单号设定
	 * 
	 * @param busNo
	 */
	public void setBusNo(String busNo) {
		if (bus)
			tprpTmainDto.put("proposalNo", busNo);// 投保单号
		this.busNo = busNo;
	}

	/**
	 * 商业险起保时间设定
	 * 
	 * @param busTimeStartDate
	 * @param busTimeStartHour
	 */
	public void setBusTimeStart(String busTimeStartDate, int busTimeStartHour) throws Exception {

		if (bus) {
			if (24 == busTimeStartHour) {
				busTimeStartDate = TimeBase.getDay(busTimeStartDate, "yyyy-MM-dd", 1);
				busTimeStartHour = 0;
			}
			this.busTimeStartDate = busTimeStartDate;
			this.busTimeStartHour = busTimeStartHour;

			tprpTmainDto.put("startDate", busTimeStartDate);// 保险起期
			tprpTmainDto.put("startHour", busTimeStartHour);// 保险起期(小时)
			if (0 == busTimeStartHour) {
				tprpTmainDto.put("endDate", TimeBase.getNextYear(busTimeStartDate + " 00:00:00", "yyyy-MM-dd"));// 保险止期
				tprpTmainDto.put("endHour", 24);// 保险止期(小时)
			} else {
				tprpTmainDto.put("endDate",
						TimeBase.getNextYear(busTimeStartDate + " "
								+ (busTimeStartHour > 9 ? ("" + busTimeStartHour) : ("0" + busTimeStartHour))
								+ ":00:00", "yyyy-MM-dd"));// 保险止期
				tprpTmainDto.put("endHour", busTimeStartHour);// 保险止期(小时)
			}
		}
	}

	/**
	 * 交强险投保单号设定
	 * 
	 * @param traNo
	 */
	public void setTraNo(String traNo) {
		if (tra)
			tprpTmainDto.put("mtplProposalNo", traNo);// 交强险投保单号
		this.traNo = traNo;
	}

	/**
	 * 交强险起保时间设定
	 * 
	 * @param traTimeStartDate
	 * @param traTimeStartHour
	 */
	public void setTraTimeStart(String traTimeStartDate, int traTimeStartHour) throws Exception {
		if (tra) {
			if(nextYearDeal(traTimeStartDate)){
				this.tra=false;
				tprpTmainDto.remove("traTimeStartDate");
				tprpTmainDto.remove("traTimeStartHour");
				tprpTmainDto.remove("mtplEndDate");
				tprpTmainDto.remove("mtplEndHour");
				tprpTmainDto.put("combineFlag", getFlag());// 联合投保标识
				tprpTmainDto.remove("mtplProposalNo");// 交强险投保单号

				for(int r=0;r<tprpTitemKindListDto.size();r++){
					if("BZ".equals(tprpTitemKindListDto.getJSONObject(r).getString("itemKindNo"))){
						tprpTitemKindListDto.remove(r);
						break;
					}
				}
				return ;
			}
			
			if (24 == busTimeStartHour) {
				traTimeStartDate = TimeBase.getDay(traTimeStartDate, "yyyy-MM-dd", 1);
				traTimeStartHour = 0;
			}
			this.traTimeStartDate = traTimeStartDate;
			this.traTimeStartHour = traTimeStartHour;

			tprpTmainDto.put("mtplStartDate", traTimeStartDate);// 交强险期间-自
			tprpTmainDto.put("mtplStartHour", traTimeStartHour);// 交强险期间-起时

			if (0 == traTimeStartHour) {
				tprpTmainDto.put("mtplEndDate", TimeBase.getNextYear(traTimeStartDate + " 00:00:00", "yyyy-MM-dd"));// 交强险期间-到
				tprpTmainDto.put("mtplEndHour", 24);// 交强险期间-止时
			} else {
				tprpTmainDto.put("mtplEndDate",
						TimeBase.getNextYear(traTimeStartDate + " "
								+ (traTimeStartHour > 9 ? ("" + traTimeStartHour) : ("0" + traTimeStartHour))
								+ ":00:00", "yyyy-MM-dd"));// 交强险期间-到
				tprpTmainDto.put("mtplEndHour", traTimeStartHour);// 交强险期间-止时
			}
		}
	}
	
	/**起保时间是否隔年
	 * @param startDate
	 * @return
	 * @throws Exception
	 */
	private boolean nextYearDeal(String startDate)throws Exception{
		int now=Integer.valueOf(TimeBase.timeToTime(TimeBase.getDay("yyyy-MM-dd", 0), "yyyy-MM-dd", "yyyy"));
		int begin=Integer.valueOf(TimeBase.timeToTime(startDate, "yyyy-MM-dd", "yyyy"));
		return begin>now;
	}

	/**
	 * 投保单信息--获取
	 * 
	 * @return
	 */
	public JSONObject getTprpTmainDto() throws Exception {
		if (!tprpTmainDto.isEmpty())
			return tprpTmainDto;
		tprpTmainDto.put("combineFlag", getFlag());// 联合投保标识
		if (bus) {
			tprpTmainDto.put("proposalNo", busNo);// 投保单号
			tprpTmainDto.put("startDate", busTimeStartDate);// 保险起期
			tprpTmainDto.put("startHour", busTimeStartHour);// 保险起期(小时)

			if (0 == busTimeStartHour) {
				tprpTmainDto.put("endDate", TimeBase.getNextYear(busTimeStartDate + " 00:00:00", "yyyy-MM-dd"));// 保险止期
				tprpTmainDto.put("endHour", 24);// 保险止期(小时)
			} else {
				tprpTmainDto.put("endDate",
						TimeBase.getNextYear(busTimeStartDate + " "
								+ (busTimeStartHour > 9 ? ("" + busTimeStartHour) : ("0" + busTimeStartHour))
								+ ":00:00", "yyyy-MM-dd"));// 保险止期
				tprpTmainDto.put("endHour", busTimeStartHour);// 保险止期(小时)
			}

		}
		if (tra) {
			tprpTmainDto.put("mtplProposalNo", traNo);// 交强险投保单号
			tprpTmainDto.put("mtplStartDate", traTimeStartDate);// 交强险期间-自
			tprpTmainDto.put("mtplStartHour", traTimeStartHour);// 交强险期间-起时

			if (0 == traTimeStartHour) {
				tprpTmainDto.put("mtplEndDate", TimeBase.getNextYear(traTimeStartDate + " 00:00:00", "yyyy-MM-dd"));// 交强险期间-到
				tprpTmainDto.put("mtplEndHour", 24);// 交强险期间-止时
			} else {
				tprpTmainDto.put("mtplEndDate",
						TimeBase.getNextYear(traTimeStartDate + " "
								+ (traTimeStartHour > 9 ? ("" + traTimeStartHour) : ("0" + traTimeStartHour))
								+ ":00:00", "yyyy-MM-dd"));// 交强险期间-到
				tprpTmainDto.put("mtplEndHour", traTimeStartHour);// 交强险期间-止时
			}
		}
		return tprpTmainDto;
	}

	/**
	 * 投保标的信息--获取
	 * 
	 * @return
	 */
	public JSONObject getTprpTitemCarDto() {
		if (!tprpTitemCarDto.isEmpty())
			return tprpTitemCarDto;
		
		//tprpTitemCarDto.put("fuelType", "0");//能源种类
		
		tprpTitemCarDto.put("runAreaCode", "32");// 行驶区域代码*重庆*
		tprpTitemCarDto.put("carKindCode", "A0");// 车辆种类(客车)
		tprpTitemCarDto.put("useNatureCode", "8A");// 车辆使用性质(家庭自用汽车)
		tprpTitemCarDto.put("whethercar", "0");// 是否外地车(1-是 0-否)
		tprpTitemCarDto.put("vehicleCategoryCode", "K33");// 交管车辆种类(默认轿车)
		tprpTitemCarDto.put("loanBeneficiary", null);// 贷款车受益人
		tprpTitemCarDto.put("loanVehicleFlag", "0");// 是否车贷投保多年标志--(默认否)

		if ("000000".equals(priceRequest.getLicense().getLicenceNo())) {
			tprpTitemCarDto.put("whethelicenses", "0");// 未上牌车辆标志代码(1-已上牌 0-未上牌)
			tprpTitemCarDto.put("purchaseDate", priceRequest.getLicense().getRegisteredTime());// 购买日期--新车时必传
		} else {
			tprpTitemCarDto.put("licenseKindCode", "02");// 号牌种类代码(默认：小型汽车号牌)
			tprpTitemCarDto.put("licenseNo", priceRequest.getLicense().getLicenceNo());// 车牌号码
			tprpTitemCarDto.put("whethelicenses", "1");// 未上牌车辆标志代码(1-已上牌 0-未上牌)
		}

		tprpTitemCarDto.put("engineNo", priceRequest.getLicense().getEngineNo());// 发动机号
		tprpTitemCarDto.put("vinNo", priceRequest.getLicense().getFrameNo());// 车架号

		tprpTitemCarDto.put("enrollDate", priceRequest.getLicense().getRegisteredTime());// 车辆初始登记日期
		if ("01".equals(priceRequest.getLicense().getTransfer())) {
			tprpTitemCarDto.put("chgOwnerFlag", "1");// 过户车辆标志
			tprpTitemCarDto.put("transferDate", priceRequest.getLicense().getTransferDate());// 转移登记日期
		} else {
			tprpTitemCarDto.put("chgOwnerFlag", "0");// 过户车辆标志
		}

		tprpTitemCarDto.put("exhaustScale", car.getString("exHaustScale"));// 排量
		tprpTitemCarDto.put("tonCount", car.getString("tonCount"));// 吨位
		tprpTitemCarDto.put("seatCount", car.getString("seatCount"));// 座位
		tprpTitemCarDto.put("modelCode", car.getString("modeCode"));// 车型代码
		if (car.getDoubleValue("exHaustScale") <= 0) {
			tprpTitemCarDto.put("fuelType", "1");// 能源种类(0燃油;1纯电动)
		}
		tprpTitemCarDto.put("purchasePrice", car.getString("purchasePrice"));// 新车购置价
		tprpTitemCarDto.put("vehicleModel", car.getString("modelName"));// 车辆型号名称

		tprpTitemCarDto.put("vinNoQueryFlag", "0");// 是否已使用VIN码解析(默认否)
		tprpTitemCarDto.put("poWeight", "1000");// 整备质量--

		return tprpTitemCarDto;
	}

	/**
	 * 车主信息--获取
	 * 
	 * @return
	 */
	public JSONObject getTprptCarOwnerDto() {
		if (!tprptCarOwnerDto.isEmpty())
			return tprptCarOwnerDto;
		tprptCarOwnerDto.put("name", priceRequest.getOwner().getName());// 客户名称--Y
		tprptCarOwnerDto.put("idType", "01");// 证件类型(默认01:身份证)
		tprptCarOwnerDto.put("gental", null);// 性别
		tprptCarOwnerDto.put("brithday", null);// 出生年月
		tprptCarOwnerDto.put("idNo", null==priceRequest.getOwner().getIdCard()?"0":priceRequest.getOwner().getIdCard());// 证件号码--Y
		tprptCarOwnerDto.put("cellPhoneNo", priceRequest.getOwner().getPhone());// 联系电话
		return tprptCarOwnerDto;
	}

	/**
	 * 投保人信息--获取
	 * 
	 * @return
	 */
	public JSONObject getTprptApplicantDto() {
		if (!tprptApplicantDto.isEmpty())
			return tprptApplicantDto;
		tprptApplicantDto = new JSONObject();
		tprptApplicantDto.put("name", priceRequest.getOwner().getName());// 客户名称--Y
		tprptApplicantDto.put("idType", "01");
		tprptApplicantDto.put("idNo", null==priceRequest.getOwner().getIdCard()?"0":priceRequest.getOwner().getIdCard());// 证件号码--Y
		tprptApplicantDto.put("cellPhoneNo", priceRequest.getOwner().getPhone());// 联系电话
		return tprptApplicantDto;
	}

	/**
	 * 被保险人信息--获取
	 * 
	 * @return
	 */
	public JSONObject getTprptInsuredDto() {
		if (!tprptInsuredDto.isEmpty())
			return tprptInsuredDto;
		
		tprptInsuredDto.put("name", priceRequest.getOwner().getName());// 客户名称--Y
		tprptInsuredDto.put("idType", "01");
		tprptInsuredDto.put("idNo", null==priceRequest.getOwner().getIdCard()?"0":priceRequest.getOwner().getIdCard());// 证件号码--Y
		tprptInsuredDto.put("cellPhoneNo", priceRequest.getOwner().getPhone());// 联系电话
		return tprptInsuredDto;
	}

	/**
	 * 险别选择信息--获取
	 * 
	 * @return
	 */
	public JSONArray getTprpTitemKindListDto() {
		if (!tprpTitemKindListDto.isEmpty())
			return tprpTitemKindListDto;
		int itemKindNo = 1;
		if (tra) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "BZ", "交强险", null, null, null, null, null));
			itemKindNo++;
		}

		if (null != getIns(InsuranceCode.BL.getCode())) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A200", "车辆损失险", null, null, null, null, "0"));
			itemKindNo++;
			if ("true".equals(getIns(InsuranceCode.BL.getCode()).getIndemnity())) {
				tprpTitemKindListDto
						.add(getInsurce(itemKindNo, "A911", "不计免赔险(机动车损失保险)", null, null, null, null, null));
				itemKindNo++;
			}
		}

		if (null != getIns(InsuranceCode.BT.getCode())) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A500", "机动车全车盗抢险", null, null, null, null, null));
			itemKindNo++;
			if ("true".equals(getIns(InsuranceCode.BT.getCode()).getIndemnity())) {
				tprpTitemKindListDto
						.add(getInsurce(itemKindNo, "A921", "不计免赔险（机动车全车盗抢保险）", null, null, null, null, null));
				itemKindNo++;
			}
		}

		if (null != getIns(InsuranceCode.BTR.getCode())) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A600", "商业第三者责任保险", null, "0", null, null,
					getIns(InsuranceCode.BTR.getCode()).getAuount()));
			itemKindNo++;
			if ("true".equals(getIns(InsuranceCode.BTR.getCode()).getIndemnity())) {
				tprpTitemKindListDto
						.add(getInsurce(itemKindNo, "A912", "不计免赔险(机动车第三者责任保险)", null, null, null, null, null));
				itemKindNo++;
			}
		}

		if (null != getIns(InsuranceCode.BD.getCode())) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A701", "机动车车上人员责任保险(司机)", null, null, null, null,
					getIns(InsuranceCode.BD.getCode()).getAuount()));
			itemKindNo++; // car.getInteger("seatCount");
			if ("true".equals(getIns(InsuranceCode.BD.getCode()).getIndemnity())) {
				tprpTitemKindListDto
						.add(getInsurce(itemKindNo, "A928", "不计免赔险(机动车车上人员责任保险(司机))", null, "0", null, null, null));
				itemKindNo++;
			}
		}

		Insurance bp = getIns(InsuranceCode.BP.getCode());
		if (null != bp) {
			BigDecimal tol = new BigDecimal(bp.getAuount());
			tol = tol.multiply(new BigDecimal(car.getInteger("seatCount") - 1));
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A702", "机动车车上人员责任保险(乘客)", bp.getAuount().toString(),
					(car.getInteger("seatCount") - 1) + "", null, null, tol.toString()));
			itemKindNo++; // ;
			if ("true".equals(bp.getIndemnity())) {
				tprpTitemKindListDto
						.add(getInsurce(itemKindNo, "A929", "不计免赔险(机动车车上人员责任保险(乘客))", null, null, null, null, null));
				itemKindNo++;
			}
		}

		if (null != getIns(InsuranceCode.BN.getCode())) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A210", "车身划痕损失险", null, null, null, null,
					getIns(InsuranceCode.BN.getCode()).getAuount().toString()));
			itemKindNo++;
			if ("true".equals(getIns(InsuranceCode.BN.getCode()).getIndemnity())) {
				tprpTitemKindListDto
						.add(getInsurce(itemKindNo, "A971", "不计免赔险（车身划痕损失险）", null, null, null, null, null));
				itemKindNo++;
			}
		}

		Insurance bg = getIns(InsuranceCode.BG.getCode());
		if (null != bg) {
			if ("01".equals(bg.getType()))
				tprpTitemKindListDto.add(getInsurce(itemKindNo, "A231", "玻璃单独破碎险", null, null, "2", "进口玻璃", null));
			else
				tprpTitemKindListDto.add(getInsurce(itemKindNo, "A231", "玻璃单独破碎险", null, null, "1", "国产玻璃", null));
			itemKindNo++;
		}

		Insurance br = getIns(InsuranceCode.BR.getCode());
		if (null != br) {
			String pate = br.getPate();

			if ("00".equals(br.getType())) {
				if (null == pate || "".equals(pate.trim()))
					pate = "30";
				tprpTitemKindListDto.add(getInsurce(itemKindNo, "A252", "指定修理厂险", pate, null, null, null, null));
				itemKindNo++;
			} else if ("01".equals(br.getType())) {
				if (null == pate || "".equals(pate.trim()))
					pate = "60";
				tprpTitemKindListDto.add(getInsurce(itemKindNo, "A252", "指定修理厂险", pate, null, null, null, null));
				itemKindNo++;
			}
		}

		if (null != getIns(InsuranceCode.BNP.getCode())) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A310", "自燃损失险", null, null, null, null, null));
			itemKindNo++;
			if ("true".equals(getIns(InsuranceCode.BNP.getCode()).getIndemnity())) {
				tprpTitemKindListDto.add(getInsurce(itemKindNo, "A970", "不计免赔险（自燃损失险）", null, null, null, null, null));
				itemKindNo++;
			}
		}

		if (null != getIns(InsuranceCode.BW.getCode())) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A290", "发动机涉水损失险", null, null, null, null, null));
			itemKindNo++;
			if ("true".equals(getIns(InsuranceCode.BW.getCode()).getIndemnity())) {
				tprpTitemKindListDto
						.add(getInsurce(itemKindNo, "A972", "不计免赔险（发动机涉水损失险）", null, null, null, null, null));
				itemKindNo++;
			}
		}
		//加上第三方特约险
		if (null != getIns(InsuranceCode.BTT.getCode())) {
			tprpTitemKindListDto.add(getInsurce(itemKindNo, "A250", "第三方特约险", null, null, null, null, null));
			itemKindNo++;
			if ("true".equals(getIns(InsuranceCode.BTT.getCode()).getIndemnity())) {
				tprpTitemKindListDto
						.add(getInsurce(itemKindNo, "A250", "不计免赔险（第三方特约险险）", null, null, null, null, null));
				itemKindNo++;
			}
		}

		return tprpTitemKindListDto;
	}

	/***
	 * 险别详情参数设定
	 * 
	 * @return
	 */
	private JSONObject getInsurce(int No, String code, String name, String price, String person, String mcode,
			String mname, String total) {
		JSONObject i = new JSONObject();
		i.put("itemKindNo", No);// 序号 --Y
		i.put("kindCodeMain", code);// 险别名称代码--Y
		i.put("kindNameMain", name);// 险别名称--Y
		if(null!=price)
		i.put("unitAmountMain", price);// 每人保额
		if(null!=person)
		i.put("quantityMain", person);// 人数--Y
		if(null!=mcode)
		i.put("modeCode", mcode);// 包装代码
		if(null!=mname)
		i.put("modeName", mname);// 包装名称
		if(null!=total)
		i.put("amountMain", total);// 总保额
		return i;
	}

	/**
	 * 获取联合投保标识
	 * 
	 * @return
	 */
	private String getFlag() {
		if (bus && tra)
			return "COMBINE";
		else {
			if (bus)
				return "MOTOR";
			if (tra)
				return "MTPL";
			else
				return "";
		}
	}
	
}
